#region Using Statements
using System;
using System.Collections.Generic;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using MyFirstXNAGame.Source.Sprites;
using MyFirstXNAGame.Source.Levels;
using MyFirstXNAGame.Source.Tiles;
#endregion

namespace MyFirstXNAGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Shooter : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;
        ArrayList sprites = new ArrayList();
        ArrayList bullets = new ArrayList();
        KeyboardState keyboard = Keyboard.GetState();
        Level level;
        SpriteBatch spriteBatch;

        public Shooter()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
            Initialize();
            //level = new Level1();
            level = new Level1();
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
        }


        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        ///             

        
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                //load the textures for the level
                for (int i = 0; i < level.tilePaths.Length; i++)
                {
                    level.tileTextures[i] = content.Load<Texture2D>(level.tilePaths[i]) as Texture2D;
                }
                for (int i = 0; i < level.spritePaths.Length; i++)
                {
                    level.spriteTextures[i] = content.Load<Texture2D>(level.spritePaths[i]) as Texture2D;
                }
                ((Level1)level).init();
                spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
            }
        }


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                // TODO: Unload any ResourceManagementMode.Automatic content
                content.Unload();
            }

            // TODO: Unload any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the default game to exit on Xbox 360 and Windows.
            ArrayList bulletCleanup = new ArrayList();
            ArrayList enemyCleanup = new ArrayList();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            level.update(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.White);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            ArrayList tiles = level.getViewFrame();
            foreach(Tile t in tiles){
                spriteBatch.Draw(t.theTexture, t.bounds, Color.White);
            }
            foreach (Sprite s in level.getSpriteFrame())
            {   
                spriteBatch.Draw(s.texture, s.getBounds(), s.getAnimationFrame(), Color.White, 0f, new Vector2(), s.spriteEffects, 0f);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
        /*
        protected void detectCollissions()
        {
            foreach (Sprite s in sprites)
            {
                if (s.bounds.Intersects(ship1.bounds))
                {
                    ship1.onCollission(s);
                    s.onCollission(ship1);
                }

                foreach (Sprite b in bullets)
                {
                    if (s.bounds.Intersects(b.bounds))
                    {
                        s.onCollission(b);
                        b.onCollission(s);
                    }
                }
            }
        }
         * */
        protected void detectAlphaCollission(Sprite a, Sprite b)
        {
            Rectangle aBounds = a.getBounds();
            Rectangle bBounds = b.getBounds();
            uint[] bitsA = new uint[a.texture.Width * a.texture.Height];
            a.texture.GetData<uint>(bitsA);

            uint[] bitsB = new uint[b.texture.Width * b.texture.Height];
            b.texture.GetData<uint>(bitsB);
            int x1 = Math.Max(aBounds.X, bBounds.X);
            int x2 = Math.Min(aBounds.X + aBounds.Width, bBounds.X + bBounds.Width);

            int y1 = Math.Max(aBounds.Y, bBounds.Y);
            int y2 = Math.Min(aBounds.Y + aBounds.Height, bBounds.Y + bBounds.Height);
            for (int y = y1; y < y2; ++y)
            {
                for (int x = x1; x < x2; ++x)
                {
                    if (((bitsA[(x - aBounds.X) + (y - aBounds.Y) * a.texture.Width] & 0xFF000000) >> 24) > 20 &&
                        ((bitsB[(x - bBounds.X) + (y - bBounds.Y) * b.texture.Width] & 0xFF000000) >> 24) > 20)
                    {
                        a.onCollission(b);
                        b.onCollission(a);
                    }
                }
            }
        }
    }
}
